package com.qdlc.p2p.biz.processor.task;


import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.processor.event.NoticeValueEvent;
import com.qdlc.p2p.common.constant.Constant;
import com.qdlc.p2p.common.constant.NoticeTplNid;
import com.qdlc.p2p.common.core.queue.ITaskProcessor;
import com.qdlc.p2p.common.core.queue.TaskProcessorException;
import com.qdlc.p2p.common.util.DataUtils;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.NoticeTypeHelper;
import com.qdlc.p2p.dal.common.NoticeTypeHelper.NidType;
import com.qdlc.p2p.dal.dto.Notice;
import com.qdlc.p2p.dal.dto.NoticeType;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.util.SystemConfigs;

/**
 * 通过模版发送短信
 * @author linya 2016-01-17
 */
@Service("noticeTemplateSmsTaskProcessor")
public class NoticeTemplateSmsTaskProcessor extends AbstractNoticeTemplateTaskProcessor {

	private static Logger logger = Logger.getLogger(NoticeTemplateSmsTaskProcessor.class);
	
	@Autowired
	@Qualifier("noticeSmsTaskProcessor")
	ITaskProcessor noticeSmsTaskProcessor;

	@Override
	protected void processInternal(String noticeTypeNid, Map<String, Object> sendData, User user) throws TaskProcessorException {
		
		//获取邮件类型，并判断是否可以发送
		NoticeType smsType = NoticeTypeHelper.getValue(noticeTypeNid, NidType.NOTICE_SMS);
		if(NoticeTplNid.NOTICE_SEND != smsType.getSend()){
        	return;
        }
		
		boolean checked = preCheck(smsType);
		
		if(!checked){
			return;
		}
		
		//获取短信必要参数
		String mobilePhone = DataUtils.toString(sendData.get("mobilePhone"));
		String title = StringUtil.fillTemplet(smsType.getTitleTemplet(), sendData);
		String content = StringUtil.fillTemplet(smsType.getTemplet(), sendData);
		
		//检测必要参数:无内容、无手机号码则不发生
		if (StringUtils.isEmpty(mobilePhone)) {
            mobilePhone = user.getMobilePhone();
        }
		if(StringUtils.isEmpty(mobilePhone) || StringUtils.isEmpty(content)){
			return;
		}
		
		//构建短信对象
        Notice sms = new Notice();
        sms.setNid(noticeTypeNid);
        sms.setType(String.valueOf(NoticeTplNid.NOTICE_SMS));
        sms.setSentUserId(SystemConfigs.ADMIN_ID);
        sms.setReceiveUserId(user.getUserId());
        sms.setTitle(title);
        sms.setContent(content);
        sms.setAddTime(DateUtil.getNow());
        sms.setReceiveAddr(mobilePhone);

        //委托处理
        NoticeValueEvent event = new NoticeValueEvent();
        event.setNotice(sms);
        noticeSmsTaskProcessor.process(event);
        
        logger.info(MessageFormat.format("++++发送给用户{0}的短信成功!", user));
	}

	private boolean preCheck(NoticeType smsType){
        if (smsType.getNid().equalsIgnoreCase("phone_code")) {
            return true;
        }
        if (smsType.getType() == Constant.SYSTEM_NOTICE) {
            return true;
        }
        return false;
	}
	
}
